using System.Text.Json.Serialization;

namespace Acme.ELeMe.SDK.Response;

public class BizResultWrapper<T>
{
    public string? cmd { get; set; }

    public string? encrypt { get; set; }

    public string? traceid { get; set; }

    public string? source { get; set; }

    public string? ticket { get; set; }

    [JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
    public int? version { get; set; }

    public long timestamp { get; set; }

    public string? sign { get; set; }

    public Body<T>? body { get; set; }

    public bool IsSuccess()
    {
        if (body?.error?.Contains("ISPInvokeError") == true)
            return false;
        if (body?.errno != 0)
            return false;

        return true;
    }

    public bool IsRetry()
    {
        if (body?.error?.Contains("ISPInvokeError") == true)
            return true;
        if (body?.errno != 0)
        {
            switch (body?.errno)
            {
                case 2001:
                //case 2002:
                case 100005:
                case 20501:
                case 20502:
                    return true;
            }
        }
        return false;
    }
}

public class Body<T>
{
    public int? errno { get; set; }

    public T? data { get; set; }

    public string? error { get; set; }

    public bool IsSuccess() => errno == 0;
}